package com.siemens.industrial.security;

import com.siemens.industrial.model.IUser;

import javax.security.auth.login.CredentialException;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by weig on 10/20/16.
 */
public abstract class AuthenticationService {
    private static final String LOGIN_SESSION_KEY = "siemens.multitenant.login.user";

    protected HttpServletRequest request;

    public AuthenticationService(HttpServletRequest request) {
        this.request = request;
    }

    public abstract Map<String,Object> checkCredential(String username, String password, String tenant);

    public void login(IUser user) {
        request.getSession().setAttribute(LOGIN_SESSION_KEY, user);
    }

    public void logout() {
        request.getSession().removeAttribute(LOGIN_SESSION_KEY);
    }

    public IUser currentUser() {
        return (IUser) request.getSession().getAttribute(LOGIN_SESSION_KEY);
    }
}
